package com.ruitu.health_2.ui.fragment;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.ruitu.arad.Arad;
import com.ruitu.arad.base.BaseFragment;
import com.ruitu.arad.base.base_list.ListBaseAdapter;
import com.ruitu.health_2.R;
import com.ruitu.health_2.ui.activity.ArtManDetailActivity;
import com.ruitu.health_2.ui.adapter.ModelArticleListAdapter;
import com.ruitu.router_module.Router;
import com.ruitu.router_module.bean.article.Article;
import com.ruitu.router_module.bean.product.ArtMan;
import com.ruitu.router_module.bean.product.VideoDetial;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

// 视频详情页面播放器下方第二个tab对应的fragment  艺人信息
public class VideoDetail2Fragment extends BaseFragment {
    private ImageView iv_header;// 艺人头像
    private TextView tv_name, tv_detail, tv_info;// 名字,标签,介绍
    private RecyclerView rcv_list;// 艺人的文章列表

    private static VideoDetial detial;// 视频详情(包含艺人信息和艺人文章列表)
    private static ArtMan artMan;// 艺人对象
    private static List<Article> articleList = new ArrayList<>();// 艺人的文章列表
    private ModelArticleListAdapter adapter;// 文章列表adapter

    public static VideoDetail2Fragment newInstance(VideoDetial detial) {
        VideoDetail2Fragment.detial = detial;
        artMan = detial.getUmsArtMan();
        articleList = detial.getArtAboutCmsArticle();

        Bundle args = new Bundle();
        VideoDetail2Fragment fragment = new VideoDetail2Fragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_video_detail2, container, false);
        iv_header = v.findViewById(R.id.iv_header);
        tv_name = v.findViewById(R.id.tv_name);
        tv_detail = v.findViewById(R.id.tv_detail);
        tv_info = v.findViewById(R.id.tv_info);
        rcv_list = v.findViewById(R.id.rcv_list);

        setArtMan();
        setArticle();
        setListeners();

        return v;
    }

    // 设置艺人相关的文章列表
    private void setArticle() {
        adapter = new ModelArticleListAdapter(getActivity());
        LinearLayoutManager manager = new LinearLayoutManager(getActivity());
        rcv_list.setLayoutManager(manager);
        rcv_list.setAdapter(adapter);
        adapter.setDataList(articleList);
    }

    // 设置艺人的信息
    private void setArtMan() {
        if (null != artMan) {
            tv_name.setText(artMan.getName());
            String tag = TextUtils.isEmpty(artMan.getIntro()) ? "暂无标签" : artMan.getIntro();
            tv_detail.setText(tag);
            tv_info.setText(artMan.getIntro());
            Glide.with(Arad.app).load(artMan.getAvaterUrl()).apply(Arad.opts_circle).into(iv_header);
        }
    }

    private void setListeners() {
        setOnClickListener(iv_header);
        adapter.setOnItemClickListener(new ListBaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View itemView, int position) {
                Bundle bundle = new Bundle();
                bundle.putString("id", articleList.get(position).getId());
                try {
                    Router.startActivity(getActivity(), "com.ruitu.health_1.ui.ArticleDetailsActivity", bundle);
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        if (v == iv_header) {
            Bundle bundle = new Bundle();
            bundle.putString("id", artMan.getId());
            bundle.putString("name", artMan.getName());
            startActivity(ArtManDetailActivity.class, bundle);
        }
    }
}
